package cn.sczc.jh.client.controller.sys;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.sczc.jh.client.base.AbstractController;
import cn.sczc.jh.client.entity.Role;
import cn.sczc.jh.client.entity.User;
import cn.sczc.jh.client.service.sys.RoleService;
import cn.sczc.jh.client.service.sys.UserService;
import cn.sczc.jh.client.vo.UserVo;

@RestController
@RequestMapping("sys/users")
public class UserController extends AbstractController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;

	/**
	 * 获取用户列表
	 * 
	 * @param modelMap
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ResponseEntity<ModelMap> list(ModelMap modelMap, @RequestParam Map<String, Object> params) {
		@SuppressWarnings("unchecked")
		Page<UserVo> page = (Page<UserVo>) super.getPage(params);
		Page<UserVo> data = userService.getUserByParams(page, params);
		return setSuccessModelMap(modelMap, data);
	}

	/**
	 * 添加用户
	 * 
	 * @param modelMap
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	public ResponseEntity<ModelMap> add(ModelMap modelMap, @RequestBody UserVo user) {
		int count = userService.addUser(user);
		return setSuccessModelMap(modelMap, count);
	}

	/**
	 * 更新用户
	 * 
	 * @param modelMap
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.PUT)
	public ResponseEntity<ModelMap> update(ModelMap modelMap, @RequestBody UserVo user) {
		int count = userService.updateUser(user);
		return setSuccessModelMap(modelMap, count);
	}

	/**
	 * 删除用户
	 * 
	 * @param modelMap
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<ModelMap> del(ModelMap modelMap, @PathVariable Long id) {
		int count = userService.delById(id);
		return setSuccessModelMap(modelMap, count);
	}

	/**
	 * 获取用户信息
	 * 
	 * @param modelMap
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResponseEntity<ModelMap> info(ModelMap modelMap, @PathVariable Long id) {
		User user = userService.getUserInfo(id);
		return setSuccessModelMap(modelMap, user);
	}
	
	
	/**
	 * 获取角色列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/roles", method = RequestMethod.GET)
	public ResponseEntity<ModelMap> list(ModelMap modelMap) {
		List<Role> data = roleService.getRoleList();
		return setSuccessModelMap(modelMap, data);
	}
}
